/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package communication;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 *
 * @author Fernando
 */
public class RequestServer {

    private String host = "http://localhost:8000/web";

    //public String table_name = "";
    public RequestServer() {
    }

    public String get(String table_name, String parameters) throws IOException {

        HttpClient httpclient = new DefaultHttpClient();

        String json = "";

        String url = this.host + "/" + table_name;

        // Prepare a request object
        if (parameters != null) {
            url = url + parameters;
        }

        System.out.println(url);
        HttpGet httpget = new HttpGet(url);

        // Execute the request
        HttpResponse response = httpclient.execute(httpget);

        // Examine the response status
        StatusLine status = response.getStatusLine();
        if(status.getStatusCode() == 204){
            
            return "";
        }
        //String s = status.toString();
        //boolean bool = s.matches("204");
        //System.out.println("S: "+s);
        //System.out.println(bool);
        System.out.println("Status: "+response.getStatusLine());
        
        // Get hold of the response entity
        HttpEntity entity = response.getEntity();

        // If the response does not enclose an entity, there is no need
        // to worry about connection release
        if (entity != null) {
            InputStream instream = entity.getContent();
            try {

                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(instream));
                // do something useful with the response

                json = reader.readLine();
            } catch (IOException ex) {

                // In case of an IOException the connection will be released
                // back to the connection manager automatically
                throw ex;

            } catch (RuntimeException ex) {

                // In case of an unexpected exception you may want to abort
                // the HTTP request in order to shut down the underlying
                // connection and release it back to the connection manager.
                httpget.abort();
                throw ex;

            } finally {

                // Closing the input stream will trigger connection release
                instream.close();

            }
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
        System.out.println("return: "+json);
        return json;
    }

    public String post(String body, String table_name) throws UnsupportedEncodingException, IOException {
        
        HttpClient httpclient = new DefaultHttpClient();
        
        String answer = "";
        
        StringEntity data = new StringEntity(body);
        
        System.out.println(body);
        
        HttpPost httppost = new HttpPost("http://localhost:8000/web/"+table_name);
        
        httppost.setHeader("Content-Type", "application/json");
        
        httppost.setEntity(data);
        
        // Execute the request
        HttpResponse response = httpclient.execute(httppost);
        
        // Examine the response status
        System.out.println("Status: "+response.getStatusLine());

        // Get hold of the response entity
        HttpEntity entity = response.getEntity();

        // If the response does not enclose an entity, there is no need
        // to worry about connection release
        if (entity != null) {
            InputStream instream = entity.getContent();
            try {

                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(instream));
                // do something useful with the response

                answer = reader.readLine();
            } catch (IOException ex) {

                // In case of an IOException the connection will be released
                // back to the connection manager automatically
                throw ex;

            } catch (RuntimeException ex) {

                // In case of an unexpected exception you may want to abort
                // the HTTP request in order to shut down the underlying
                // connection and release it back to the connection manager.
                httppost.abort();
                throw ex;

            } finally {

                // Closing the input stream will trigger connection release
                instream.close();

            }
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
        System.out.println("return: "+answer);
        return answer;
    }
    
    public String put(String table_name, String body, String parameters) throws UnsupportedEncodingException, IOException{
        
        HttpClient httpclient = new DefaultHttpClient();
        
        String answer = "";
        
        StringEntity data = new StringEntity(body);
        
        String url = this.host + "/" + table_name;

        // Prepare a request object
        if (parameters != null) {
            url = url + parameters;
        }        
        
        HttpPut httpput = new HttpPut(url);
        
        httpput.setHeader("Content-Type", "application/json");
        
        httpput.setEntity(data);
        
        // Execute the request
        HttpResponse response = httpclient.execute(httpput);
        
        // Examine the response status
        System.out.println(response.getStatusLine());

        // Get hold of the response entity
        HttpEntity entity = response.getEntity();

        // If the response does not enclose an entity, there is no need
        // to worry about connection release
        if (entity != null) {
            InputStream instream = entity.getContent();
            try {

                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(instream));
                // do something useful with the response

                answer = reader.readLine();
            } catch (IOException ex) {

                // In case of an IOException the connection will be released
                // back to the connection manager automatically
                throw ex;

            } catch (RuntimeException ex) {

                // In case of an unexpected exception you may want to abort
                // the HTTP request in order to shut down the underlying
                // connection and release it back to the connection manager.
                httpput.abort();
                throw ex;

            } finally {

                // Closing the input stream will trigger connection release
                instream.close();

            }
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
        System.out.println("return: "+answer);
        return answer;
    }
    
    public String delete(String table_name, String parameters) throws IOException{

        HttpClient httpclient = new DefaultHttpClient();

        String json = "";

        String url = this.host + "/" + table_name;

        // Prepare a request object
        if (parameters != null) {
            url = url + parameters;
        }

        System.out.println(url);
        HttpDelete httpdelete = new HttpDelete(url);

        // Execute the request
        HttpResponse response = httpclient.execute(httpdelete);
        // Examine the response status
        StatusLine status = response.getStatusLine();
        
        System.out.println(response.getStatusLine());

        // Get hold of the response entity
        HttpEntity entity = response.getEntity();

        // If the response does not enclose an entity, there is no need
        // to worry about connection release
        if (entity != null) {
            InputStream instream = entity.getContent();
            try {

                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(instream));
                // do something useful with the response

                json = reader.readLine();
            } catch (IOException ex) {

                // In case of an IOException the connection will be released
                // back to the connection manager automatically
                throw ex;

            } catch (RuntimeException ex) {

                // In case of an unexpected exception you may want to abort
                // the HTTP request in order to shut down the underlying
                // connection and release it back to the connection manager.
                httpdelete.abort();
                throw ex;

            } finally {

                // Closing the input stream will trigger connection release
                instream.close();

            }
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
        System.out.println("return: "+json);
        return json;        
    }
}
